package cn.yangliu.source;

import cn.yangliu.bean.EntitySetting;
import cn.yangliu.bean.ProjectSetting;
import cn.yangliu.comm.tools.StringUtils;
import cn.yangliu.tools.DBUtils;

import java.util.Arrays;
import java.util.List;

public class EntitySource extends CodeSource {

    private String baseClassFullName;

    private Boolean equalAndHash;

    private Boolean toString;

    private Boolean noArgConstructor;

    private Boolean chain;

    private Boolean useBaseClass;

    private String baseClassShortName;

    private String baseClassPackage;

    private DBUtils.TableInfo tableInfo;

    private String primaryKeyName;

    private List<String> excludeColumns;

    private String primaryKeyType;

    public EntitySource(ProjectSetting projectSetting, EntitySetting entitySetting, String entityName, DBUtils.TableInfo tableInfo) {
        super(projectSetting, entitySetting.getEntityPackage(), projectSetting.getCodePath());
        this.tableInfo = tableInfo;
//        this.mapping = entitySetting.getMapping();
        this.primaryKeyName = entitySetting.getPrimaryKeyName();
        this.primaryKeyType = entitySetting.getPrimaryKeyType();
        init(entitySetting, entityName);
    }

    private void init(EntitySetting entitySetting, String entityName) {

        String[] split = entitySetting.getExcludeColumns().split(",");

        excludeColumns = Arrays.asList(split);

        equalAndHash = entitySetting.getEqualAndHash();

        toString = entitySetting.getToString();

        noArgConstructor = entitySetting.getNoArgConstructor();

        chain = entitySetting.getChain();

        useBaseClass = entitySetting.getUseBaseClass();

        baseClassFullName = entitySetting.getBaseClassFullName();

        if (baseClassFullName.endsWith(".")) {
            throw new IllegalArgumentException("基类参数非法");
        }
        if (baseClassFullName.contains(".")) {
            baseClassPackage = baseClassFullName.substring(0, baseClassFullName.lastIndexOf("."));
            baseClassShortName = baseClassFullName.substring(baseClassFullName.lastIndexOf(".") + 1);
        }

        filename = entityName + ".java";

        shortName = entityName;

        if (StringUtils.isNotEmpty(fullPackage)) {
            classFullName = fullPackage + "." + entityName;
        } else {
            classFullName = entityName;
        }
    }


    public String getBaseClassFullName() {
        return baseClassFullName;
    }

    public void setBaseClassFullName(String baseClassFullName) {
        this.baseClassFullName = baseClassFullName;
    }

    public Boolean getEqualAndHash() {
        return equalAndHash;
    }

    public void setEqualAndHash(Boolean equalAndHash) {
        this.equalAndHash = equalAndHash;
    }

    public Boolean getToString() {
        return toString;
    }

    public void setToString(Boolean toString) {
        this.toString = toString;
    }

    public Boolean getNoArgConstructor() {
        return noArgConstructor;
    }

    public void setNoArgConstructor(Boolean noArgConstructor) {
        this.noArgConstructor = noArgConstructor;
    }

    public Boolean getChain() {
        return chain;
    }

    public void setChain(Boolean chain) {
        this.chain = chain;
    }

    public Boolean getUseBaseClass() {
        return useBaseClass;
    }

    public void setUseBaseClass(Boolean useBaseClass) {
        this.useBaseClass = useBaseClass;
    }

    public String getBaseClassShortName() {
        return baseClassShortName;
    }

    public void setBaseClassShortName(String baseClassShortName) {
        this.baseClassShortName = baseClassShortName;
    }

    public String getBaseClassPackage() {
        return baseClassPackage;
    }

    public void setBaseClassPackage(String baseClassPackage) {
        this.baseClassPackage = baseClassPackage;
    }

    public DBUtils.TableInfo getTableInfo() {
        return tableInfo;
    }

    public void setTableInfo(DBUtils.TableInfo tableInfo) {
        this.tableInfo = tableInfo;
    }

    public String getPrimaryKeyName() {
        return primaryKeyName;
    }

    public void setPrimaryKeyName(String primaryKeyName) {
        this.primaryKeyName = primaryKeyName;
    }

    public List<String> getExcludeColumns() {
        return excludeColumns;
    }

    public void setExcludeColumns(List<String> excludeColumns) {
        this.excludeColumns = excludeColumns;
    }

    public String getPrimaryKeyType() {
        return primaryKeyType;
    }

    public void setPrimaryKeyType(String primaryKeyType) {
        this.primaryKeyType = primaryKeyType;
    }
}
